package servlet;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import domian.Result;
import domian.ResultStatus;
import domian.User;
import redis.clients.jedis.Jedis;
import service.UpdateService;
import service.UserService;
import service.impl.UpdateServiceImpl;
import service.impl.UserServiceImpl;
import utils.Redis;
import utils.ToolSet;
import utils.Utils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@WebServlet("/UpdateServlet/*")
public class UpdateServlet extends BaseServlet{
    //用户修改数据服务层
    UpdateService us = new UpdateServiceImpl();

    ObjectMapper om = new ObjectMapper();

    UserService use = new UserServiceImpl();

    Jedis jedis = new Jedis();
    /**
     * 修改昵称
     */
    public void updateNick(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取请求参数
        String nickName = req.getParameter("nickname");

        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        //从redis中获得user
        String json = jedis.get(redisKey);
        User user = om.readValue(json, User.class);

        //给user的昵称赋值
        user.setNickname(nickName);
        //调用service修改数据
        Result result = us.updateNick(user);
        //修改后同时更新redis中的数据
        use.userUpdate(user,redisKey);
        //将数据返回前端
        ToolSet.printWriter(resp,result);
    }

    /**
     * 修改性别
     * @param req
     * @param resp
     * @throws IOException
     */
    public void updateSex(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取请求参数
        String selectedGender = req.getParameter("gender");

        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        //从redis中获得user
        String json = jedis.get(redisKey);
        //当前key值的存活时间
        User user = om.readValue(json, User.class);

        //给user的性别赋值
        user.setSex(selectedGender);
        //调用service修改数据
        Result result = us.updateSex(user);
        //修改后同时更新redis中的数据
        use.userUpdate(user,redisKey);
        //将数据返回前端
        ToolSet.printWriter(resp,result);
    }

    /**
     * 修改邮箱
     * @param req
     * @param resp
     * @throws IOException
     */
    public void updateEmail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultStatus rts = null;
        Result result = null;
        //获取请求参数
        String oldemail = req.getParameter("oldemail");
        String newemail = req.getParameter("newemail");

        //邮箱校验
        String emailRegex = "^[A-Za-z0-9+_.-]+@(.+)$";
        Pattern pattern = Pattern.compile(emailRegex);
        Matcher matcher = pattern.matcher(newemail);

        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        //从redis中获得user
        String json = jedis.get(redisKey);
        User user = om.readValue(json, User.class);

        boolean flag = user.getEmail() == null && oldemail == "";

        if ((flag || oldemail.equals(user.getEmail())) && matcher.matches()){
            //如果和原始邮箱相等则更改为新的邮箱
            user.setEmail(newemail);
            //调用service修改数据
            result = us.updateEmail(user);
            //修改后同时更新redis中的数据
            use.userUpdate(user,redisKey);
        }else {
            rts = ResultStatus.UPDATE_ERROR;
            result = new Result();
            result.setCode(rts.getCode());
            result.setMsg(rts.getMsg());
        }
        //将数据返回前端
        ToolSet.printWriter(resp,result);
    }


    /**
     * 修改密码
     * @param req
     * @param resp
     * @throws IOException
     */
    public void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultStatus rts = null;
        Result result = new Result();
        //获取请求参数
        String oldPassword = Utils.md5(req.getParameter("oldPassword"));
        String newPassword = Utils.md5(req.getParameter("newPassword"));

        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        //从redis中获得user
        String json = jedis.get(redisKey);
        User user = om.readValue(json, User.class);

        String userpassword = user.getPassword();

        if (userpassword.equals(oldPassword)){
            //旧密码匹配的话，新密码存入数据库
            user.setPassword(newPassword);
            Result result1 = us.updatePassword(user);
            jedis.del(redisKey);
            //将数据返回前端
            ToolSet.printWriter(resp,result1);
        }else {
            //旧密码不匹配
            rts = ResultStatus.UPDATE_ERROR;
            result.setCode(rts.getCode());
            result.setMsg("旧密码输入错误。");
            //将数据返回前端
            ToolSet.printWriter(resp,result);
        }
    }
}
